package com.eascs.ticket.ticket.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.ObjectUtil;
import com.eascs.ticket.common.entity.TicketFrom;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.common.service.TicketServiceInterface;
import com.eascs.ticket.ticket.entity.Ticket;
import com.eascs.ticket.user.entity.User;
import com.eascs.ticket.user.service.UserService;

@Service("localApi")
public class TicketServiceApiLocal extends BaseSevice implements TicketServiceInterface {
	private static Logger log = LoggerFactory.getLogger(TicketServiceApiLocal.class);
	@Inject
	private TicketService ticketService;
	@Inject
	private UserService userService;

	@Transactional(transactionManager = "ticketTransactionManager")
	public int insertTicket(TicketFrom ticketFrom) {
		Ticket ticket = new Ticket();
		ObjectUtil.copySimpleProperties(ticketFrom, ticket);
		ticket.setDueDate(new java.sql.Date(ticketFrom.getDueDate().getTime()));
		ticket.setImplementationReviewTime(new java.sql.Date(ticketFrom.getImplementationReviewTime().getTime()));
		int flag = 0;
		if (ticketService.getTicketById(ticket.getId()) == null) {
			flag = proxyDao.insert(ticket);
			log.info("ticket.getReporter()===" + ticket.getReporter());
			log.info("user===" + userService.getUserByCode(ticket.getReporter()));
			log.info("ticket.getImplementationReviewTime()===" + ticket.getImplementationReviewTime());
			// 发邮件给IT处理人
			User user = userService.getUserByCode(ticket.getReporter());
			if (user != null) {
				ticketService.sendMail(ticket, user, "addTicket");
				log.info("邮件已发送" + user.getEmail());
			} else {
				log.error("工单单号:[" + ticket.getId() + "],邮件发送失败，IT处理人不存在");
			}
		}
		if (flag > 0) {
			log.info("工单单号:[" + ticket.getId() + "],创建成功");
		} else {
			log.error("工单单号:[" + ticket.getId() + "],创建失败");
		}
		return flag;
	}

	@Override
	public List<Object[]> findObjectsList(String sql, Object... params) {
		return this.proxyDao.findObjectsList(sql, params);
	}
}
